// API响应基础类型
export interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
}

export interface ApiErrorResponse {
  success: false
  error: string
}

// 分页类型
export interface Pagination {
  page: number
  limit: number
  total: number
  pages: number
}

// 用户相关类型
export interface User {
  id: string
  username: string
  email: string
  role: 'ADMIN' | 'EDITOR' | 'VIEWER'
  avatar?: string
  status: 'ACTIVE' | 'INACTIVE' | 'BANNED'
  lastLoginAt?: string
  createdAt: string
  updatedAt: string
}

export interface UserCreateData {
  username: string
  email: string
  password: string
  role?: 'ADMIN' | 'EDITOR' | 'VIEWER'
  status?: 'ACTIVE' | 'INACTIVE' | 'BANNED'
}

export interface UserUpdateData {
  username?: string
  email?: string
  role?: 'ADMIN' | 'EDITOR' | 'VIEWER'
  status?: 'ACTIVE' | 'INACTIVE' | 'BANNED'
  avatar?: string
}

// 用户资料扩展类型
export interface UserProfile extends User {
  fullName?: string
  phone?: string
  department?: string
  position?: string
  bio?: string
  location?: string
  website?: string
  skills?: string[]
}

export interface ProfileUpdateData {
  fullName?: string
  phone?: string
  department?: string
  position?: string
  bio?: string
  location?: string
  website?: string
  skills?: string[]
}

export interface UserListResponse {
  users: User[]
  pagination: Pagination
}

export interface UserStats {
  totalUsers: number
  activeUsers: number
  bannedUsers: number
  usersByRole: Array<{ role: string; _count: { role: number } }>
  recentUsers: User[]
}

// 知识库相关类型
export interface KnowledgeBase {
  id: string
  title: string
  description?: string
  content: string
  slug: string
  excerpt?: string
  status: 'DRAFT' | 'PUBLISHED' | 'ARCHIVED'
  featured: boolean
  viewCount: number
  likeCount: number
  publishedAt?: string
  createdAt: string
  updatedAt: string
  author: {
    id: string
    username: string
    avatar?: string
  }
  category?: {
    id: string
    name: string
    description?: string
    color: string
  }
  tags: Array<{
    id: string
    name: string
    description?: string
    color: string
  }>
  likes?: Array<{
    user: {
      id: string
      username: string
      avatar?: string
    }
  }>
  comments?: Array<{
    id: string
    content: string
    createdAt: string
    author: {
      id: string
      username: string
      avatar?: string
    }
  }>
}

export interface KnowledgeBaseCreateData {
  title: string
  description?: string
  content: string
  categoryId?: string
  tagIds?: string[]
  status?: 'DRAFT' | 'PUBLISHED'
  featured?: boolean
}

export interface KnowledgeBaseUpdateData {
  title?: string
  description?: string
  content?: string
  categoryId?: string
  tagIds?: string[]
  status?: 'DRAFT' | 'PUBLISHED' | 'ARCHIVED'
  featured?: boolean
}

export interface KnowledgeBaseListResponse {
  knowledgeBases: KnowledgeBase[]
  pagination: Pagination
}

export interface KnowledgeBaseStats {
  totalKnowledgeBases: number
  publishedKnowledgeBases: number
  draftKnowledgeBases: number
  featuredKnowledgeBases: number
  totalViews: number
  totalLikes: number
  recentKnowledgeBases: KnowledgeBase[]
}

// 分类相关类型
export interface Category {
  id: string
  name: string
  description?: string
  slug: string
  icon?: string
  color: string
  order: number
  parentId?: string
  createdAt: string
  updatedAt: string
  createdBy: {
    id: string
    username: string
  }
  children?: Category[]
  parent?: {
    id: string
    name: string
    slug: string
  }
  _count?: {
    knowledgeBases: number
    children: number
  }
}

export interface CategoryCreateData {
  name: string
  description?: string
  icon?: string
  color?: string
  order?: number
  parentId?: string
}

export interface CategoryUpdateData {
  name?: string
  description?: string
  icon?: string
  color?: string
  order?: number
  parentId?: string
}

export interface CategoryReorderData {
  id: string
  order: number
  parentId?: string
}

export interface CategoryStats {
  totalCategories: number
  topLevelCategories: number
  categoriesWithChildren: number
  mostUsedCategories: Array<{
    id: string
    name: string
    color: string
    _count: { knowledgeBases: number }
  }>
  recentCategories: Array<{
    id: string
    name: string
    slug: string
    createdAt: string
  }>
}

// 标签相关类型
export interface Tag {
  id: string
  name: string
  description?: string
  slug: string
  color: string
  createdAt: string
  updatedAt: string
  createdBy: {
    id: string
    username: string
  }
  knowledgeBases?: Array<{
    id: string
    title: string
    status: string
    createdAt: string
  }>
  _count?: {
    knowledgeBases: number
  }
}

export interface TagCreateData {
  name: string
  description?: string
  color?: string
}

export interface TagUpdateData {
  name?: string
  description?: string
  color?: string
}

export interface TagBatchCreateData {
  tags: TagCreateData[]
}

export interface TagBatchDeleteData {
  tagIds: string[]
}

export interface TagListResponse {
  tags: Tag[]
  pagination: Pagination
}

export interface TagBatchResult {
  createdTags?: Tag[]
  deletedTags?: Array<{ id: string; name: string }>
  errors: Array<{ id?: string; name?: string; error: string }>
  summary: {
    total: number
    success: number
    failed: number
  }
}

export interface TagStats {
  totalTags: number
  mostUsedTags: Array<{
    id: string
    name: string
    color: string
    _count: { knowledgeBases: number }
  }>
  recentTags: Array<{
    id: string
    name: string
    color: string
    createdAt: string
  }>
  tagsByMonth: Array<{
    createdAt: string
    _count: { createdAt: number }
  }>
}

// 文件相关类型
export interface FileInfo {
  id: string
  filename: string
  originalName: string
  mimeType: string
  size: number
  path: string
  url: string
  createdAt: string
  updatedAt: string
  uploadedBy: {
    id: string
    username: string
  }
}

export interface FileListResponse {
  files: FileInfo[]
  pagination: Pagination
}

export interface FileStats {
  totalFiles: number
  totalSize: number
  filesByType: Array<{
    mimeType: string
    count: number
    totalSize: number
  }>
  recentFiles: FileInfo[]
  scope?: 'all' | 'user' // 统计范围：全部文件或用户文件
}

// 搜索相关类型
export interface SearchParams {
  query: string
  type?: string
  page?: number
  limit?: number
  filters?: any
}

export interface SearchResponse {
  results: any[]
  total: number
  pagination: Pagination
  suggestions?: string[]
}

// 分析相关类型
export interface AnalyticsOverview {
  totalUsers: number
  totalKnowledgeBases: number
  totalViews: number
  totalLikes: number
  activeUsers: number
  publishedContent: number
}

export interface TrendData {
  date: string
  users: number
  views: number
  likes: number
  content: number
}

export interface TopContent {
  id: string
  title: string
  type: string
  views: number
  likes: number
  createdAt: string
}

export interface UserBehavior {
  pageViews: number
  uniqueVisitors: number
  averageSessionDuration: number
  bounceRate: number
  topPages: Array<{
    path: string
    views: number
    uniqueViews: number
  }>
}

export interface ContentAnalysis {
  contentByStatus: Array<{
    status: string
    count: number
    percentage: number
  }>
  contentByCategory: Array<{
    category: string
    count: number
    percentage: number
  }>
  contentByMonth: Array<{
    month: string
    count: number
  }>
}

export interface UserGrowth {
  newUsers: Array<{
    date: string
    count: number
  }>
  activeUsers: Array<{
    date: string
    count: number
  }>
  userRetention: Array<{
    period: string
    rate: number
  }>
}

// 认证相关类型
export interface LoginCredentials {
  username: string
  password: string
}

export interface RegisterData {
  username: string
  email: string
  password: string
  role?: 'ADMIN' | 'EDITOR' | 'VIEWER'
}

export interface AuthResponse {
  user: User
  token: string
}

export interface RefreshTokenData {
  token: string
}

// 通用响应类型
export type ApiSuccessResponse<T> = ApiResponse<T>
export type ApiFailureResponse = ApiErrorResponse

// 联合类型
export type AnyApiResponse<T> = ApiSuccessResponse<T> | ApiFailureResponse

// 工具类型
export type ExtractApiData<T> = T extends ApiResponse<infer U> ? U : never
export type ExtractApiError<T> = T extends ApiErrorResponse ? T['error'] : never
